﻿
GY.Bill.SmallTicket = {
    GetXiaoPiaolModel: function () {
        var model = {
            BankId: $("#txtBank").attr("data-val"),
            BillAmountt: $("#txtAmountt").val(),
            EndDate: $("#txtEndDate").val(),
            BillCount: $("#txtCount").text()
            //BillLocation: $("#txtLocation").val()
        }
        return model;
    },
    //保存票据
    SaveBill: function () {
        var returnUrl = $("#divMain").attr("data-returnurl");
        $.ajax({
            type: "Post",
            url: $("#divMain").attr("data-url"),
            data: GY.Bill.SmallTicket.GetXiaoPiaolModel(),
            success: function (result) {
                location.href = returnUrl;
            }
        });
    },
    //删除票据
    DeleteBill: function ($this) {
        var id= $this.attr("data-id");
        $.ajax({
            type: "Post",
            url: "DeleteXiaoPiao?id="+id,
            success: function (result) {
                if (result) {
                    $this.parent().parent().parent().parent().parent().remove();
                }
            }
        });
    },
    
    //发布票据
    PublistBill: function () {
        //单个票据的单价
        
        var billamount = 0;
        var billcount = 0;
        var bankId = 0;
        var billamountall = 0;
        var remark = $("#txtRemark").val();
        var billlocation = $("#txtLocation").val();

        

        $(".tritem").each(function () {
            //银行不知道取哪一个，默认取最后一个
            bankId = $(this).attr("data-bankId");
            var singlebillamount = parseFloat($(this).attr("data-billAmount"));
            var singlebillcount = parseInt($(this).attr("data-billCount"));
            billamount += parseFloat(singlebillamount * singlebillcount);
            //billamount += parseInt($(this).attr("data-billAmount"));
            billcount += singlebillcount;
        });
        
        var model = {
            BankId: bankId,
            BillAmountt: billamount,
            BillCount: billcount,
            Remark: remark,
            BillLocation: billlocation
        }
        var returnUrl = $("#divMain").attr("data-successUrl");
        var submitUrl = $("#divMain").attr("data-submitUrl");
        $.ajax({
            type: "Post",
            url: submitUrl,
            data: model,
            success: function (result) {
                //
                if (result.code == 1) {
                    location.href = returnUrl.replace("xiaopiaogourpId", result.data).replace("xiaopiaogourpId", result.data);
                }
                else {
                    alert("发布失败");
                }
                
            }
        });
        
    },
    CloseConsult: function () {
        var $this = $("#btnClose");
        $.ajax({
            type: "Post",
            url: $this.attr("data-url"),
            data: { id: $("#divMain").attr("data-id") },
            dataType: "json",
            async: false,
            success: function (result) {
                if (result.code == 1) {
                    window.location.href = $(".back").attr("href");
                }
            }
        });
    },
    ValidDataResult: function () {
        var BankId = $("#txtBank").attr("data-val");
        var BillAmountt = $("#txtAmountt").val();
        var EndDate = $("#txtEndDate").val();
        var BillCount = $("#txtCount").text();
        //var BillLocation = $("#txtLocation").val();
        var msg = "";
        if (BankId == "") {
            msg = "请选择付款行";
            return msg;
        }
        if (BillAmountt == "" || (BillAmountt / 1 <= 0)) {
            msg = "请输入正确的金额";
            return msg;
        }
        if (EndDate == "") {
            msg = "请选择到期日";
            return msg;
        }
        if (BillCount == "") {
            msg = "请选择张数";
            return msg;
        }
        return msg;
    },
    //获取热门银行
    GetHotBank: function () {
        var $divHot = $("#div_bank_hot");
        $.ajax({
            type: "Post",
            url: $divHot.attr("data-url"),
            data: null,
            success: function (result) {
                var items = [];
                if (result.code == 1) {
                    for (var i = 0; i < result.data.length; i++) {
                        items.push("<a href=\"javascript:;\" data-val=\"" + result.data[i]["Id"] + "\" class=\"fl\">"
                        + result.data[i]["BankName"] + "</a>");
                    }
                }
                $divHot.html(items.join(""));
            }
        });
    },
    GetBankByKey: function (key) {
        var $divList = $("#div_bank_result_list");
        $.ajax({
            type: "Post",
            url: $divList.attr("data-url"),
            data: { key: key },
            success: function (result) {
                if (result.code == 1) {
                    var items = [];
                    if (result.code == 1) {
                        for (var i = 0; i < result.data.length; i++) {
                            items.push("<a href=\"javascript:;\" data-val=\"" + result.data[i]["Id"] + "\" class=\"fl\">"
                            + result.data[i]["BankName"] + "</a>");
                        }
                    }
                    $divList.html(items.join(""));
                }
            }
        });
    }
};

(function () {
    $(function () {
        //弹窗
        //增加
        $(".add").on("tap", function () {
            var oldval = $("#txtCount").text();
            if (oldval != null && oldval != undefined) {
                $("#txtCount").text(parseInt(oldval) + 1);
            }
        });
        //减少
        $(".cut").on("tap", function () {
            var oldval = $("#txtCount").text();
            if (oldval != null && oldval != undefined && oldval/1 > 1) {
                $("#txtCount").text(parseInt(oldval) - 1);
            }
        });
        //保存
        $("#BtnSave").on("tap", function () {
            var msg = GY.Bill.SmallTicket.ValidDataResult();
            if (msg == "") {
                GY.Bill.SmallTicket.SaveBill();
            }
            else {
                ShowTip(msg);
                return;
            }
        });
        //删除
        $(".del").on("tap", function () {
            
            $this = $(this);
            //var id = $(this).attr("data-id");
            GY.Bill.SmallTicket.DeleteBill($this);
        });


        //发布弹出提交窗口
        $("#BtnPublist").on("tap", function () {
            if ($(".tritem").length == 0) {
                msg = "请添加一张小票";
                ShowTip(msg);
                return;
            }
            else {
                if ($("#txtLocation").val()=="") {
                    msg = "请填写票在哪儿";
                    ShowTip(msg);
                    return;
                }
                fnShade($(".shade"));
                $(".shade").show();
                $(".mask_tip").show();
            }
        });
        //确认发布
        $("#BtnSurePublist").on("tap", function () {
            var flag = GY.Bill.Common.SaveCheck($(this));
            if (flag) {
                GY.Bill.SmallTicket.PublistBill();
            }
        });

        //修改用户信息并保存报价信息
        $("#btnSaveUserOffer").on("tap", function () {
            var flag = GY.Bill.Common.SaveUser();
            if (flag) {
                $("#BtnSurePublist").attr("data-check", 1);
                GY.Bill.SmallTicket.PublistBill();
            }
        });


        //弹窗
        $("#btnClose").on("tap", function () {
            ShowPopup("是否确认关闭当前询价？", "确认");
        });

        $("#btnSure").on("tap", function () {
            GY.Bill.SmallTicket.CloseConsult();
        });


        //选择银行
        $("#txtBank").focus(function () {
            $("#div_bank_search").show();
            $("#divMain").hide();

            GY.Bill.SmallTicket.GetHotBank();
        });

        $("#btn_bank_goback").click(function () {
            $("#divMain").show();
            $("#div_bank_search").hide();
        });

        //搜索银行
        $("#btnSearch").on("click", function () {
            var key = $("#txtBankKey").val();

            if (key == "") {
                return;
            }

            $("#div_bank_result").show();
            $("#div_bank_search").hide();

            GY.Bill.SmallTicket.GetBankByKey(key);
        });

        $("#btnSearchResult").on("click", function () {
            var key = $("#txtBankResultKey").val();
            if (key == "") {
                return;
            }

            $("#div_bank_result").show();
            $("#div_bank_search").hide();
            GY.Bill.SmallTicket.GetBankByKey(key);
        });

        $("#btn_bankResult_goback").click(function () {
            $("#div_bank_search").show();
            $("#div_bank_result").hide();
        });

        //点击银行赋值
        $(".banklist").on("click", "a", function () {
            $("#div_bank_search").hide();
            $("#div_bank_result").hide();
            $("#divMain").show();
            var $this = $(this);
            $("#txtBank").val($this.text());
            $("#txtBank").attr("data-val", $this.attr("data-val"));
        });

        

        
        
    });
})();

